package utils;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;

public class JDBCTools {

    static String className = null;
    static String url = null;
    static String user = null;
    static String password = null;


    /**
     * 链接数据库
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getCon() throws ClassNotFoundException, SQLException {
        Class.forName(className);
        Connection con = DriverManager.getConnection(url,user,password);
        return con;
    }

    /**
     * 关闭 (查询)
     * @param con
     * @param pstm
     * @param rs
     */
    public static void close(Connection con , PreparedStatement pstm , ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(pstm != null){
            try {
                pstm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭(增删改)
     * @param con
     * @param pstm
     */
    public static void close(Connection con , PreparedStatement pstm){
        if(pstm != null){
            try {
                pstm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
